//
//  HDAnnotationView.m
//  HDMapKit
//
//  Created by Liuzhuan on 13-4-8.
//  Copyright (c) 2013年 hengdawb. All rights reserved.
//

#import "HDAnnotationView.h"
#import "HDCallOutView.h"
#import <QuartzCore/QuartzCore.h>
#import "HDDeclare.h"
#import "UIView+Extension.h"
//#import "HDScrollLabel.h"

#import "UIButton+WebCache.h"

@interface HDAnnotationView ()



@end

static NSString *pointHei = @"mapPointHei";
static NSString *pointNor = @"mapPointNor";

@implementation HDAnnotationView
{
    BOOL _canStartAnimation;
    float _lastBili;
}


- (id)initWithAnnotation:(HDAnnotation *)annotation animated:(BOOL)animate {
    
	if ((self = [super initWithFrame:CGRectZero])) {
        
        self.annotation = annotation;

        float pinWidth = 30;
        float pinHeight = pinWidth * 1.25;
        
        _canStartAnimation = YES;
        
        self.clipsToBounds = NO;
        [self setImage:[UIImage imageNamed:@"pinRed"] forState:UIControlStateNormal];
//        [self sd_setBackgroundImageWithURL:[NSURL URLWithString:self.annotation.imageUrl] forState:UIControlStateNormal];
        [self addTarget:self action:@selector(showCallOut) forControlEvents:UIControlEventTouchUpInside];
        
        if (annotation.mineAnn) {
            self.enabled = NO;
            self.frame = CGRectMake(0, 0, pinWidth, pinWidth);
//            [self setBackgroundImage:[UIImage imageNamed:@"mapLocationPin"] forState:UIControlStateNormal];
            self.center = self.annotation.point;
            
            [self shakeToShow:self];
        }
        else
        {
            self.frame = CGRectMake(0, 0, pinWidth, pinHeight);
            self.center = self.annotation.point;
//            CABasicAnimation *pindrop = [CABasicAnimation animationWithKeyPath:@"position.y"];
//            pindrop.duration       = 0.5f;
//            pindrop.fromValue      = [NSNumber numberWithFloat:self.center.y - kScreenHeight];
//            pindrop.toValue        = [NSNumber numberWithFloat:self.center.y];
//            pindrop.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//            [self.layer addAnimation:pindrop forKey:@"pindrop"];
        }
	}
    
//    NSLog(@"originalFram:%@",NSStringFromCGRect(self.centerImage.frame));
    
	return self;
}

- (void) shakeToShow:(UIView*)aView
{
    
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    animation.duration = 2.0;// 动画时间
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    
    // 这三个数字，我只研究了前两个，所以最后一个数字我还是按照它原来写1.0；前两个是控制view的大小的；
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1.0)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1.0)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    
    animation.values = values;
    
    [aView.layer addAnimation:animation forKey:nil];
    
}

-(void)showCallOut
{
    [self.myDelegate heighlightAnnotationWith:self];
    
    self.transform = CGAffineTransformMakeScale(1.5, 1.5);
}

-(void)showHeigh
{
    self.transform = CGAffineTransformMakeScale(1.5, 1.5);
}

-(void)showNormal
{
    self.transform = CGAffineTransformMakeScale(1.0, 1.0);
}


#pragma mark - 动画方法
-(void)startAnimationWith:(float)bili
{
    self.hidden = NO;
    
    CABasicAnimation *scaoleAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    scaoleAnimation.duration = 0.8;
    scaoleAnimation.autoreverses = YES;
    scaoleAnimation.repeatCount = MAXFLOAT;
    // 设定动画起始帧和结束帧
//    scaoleAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)]; // 起始点
//    scaoleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y + 10)]; // 终了点
    
    scaoleAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.annotation.point.x*_lastBili, self.annotation.point.y*_lastBili)]; // 起始点
    scaoleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.annotation.point.x*_lastBili, self.annotation.point.y*_lastBili + 10)];
    
    NSLog(@"跳动的坐标：%@",NSStringFromCGPoint(CGPointMake(self.annotation.point.x*bili, self.annotation.point.y*bili)));
    
//    scaoleAnimation.fillMode = kCAFillModeForwards;
//    scaoleAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
    [self.layer addAnimation:scaoleAnimation forKey:@"tiaoAnimation"];
    
    scaoleAnimation.removedOnCompletion = NO;
    scaoleAnimation.fillMode = kCAFillModeForwards;
    
//    scaoleAnimation.fillMode=kCAFillModeForwards;
//    scaoleAnimation.removedOnCompletion = NO;
}

-(void)stopAnimation
{
    [self.layer removeAllAnimations];
}

#pragma mark - 观察者方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
	if ([keyPath isEqual:@"contentSize"]) {        
        NSString *biliStr = (NSString*)[object valueForKey:@"zoomScale"];
        float bili = biliStr.floatValue;
        self.center = CGPointMake(self.annotation.point.x*bili, self.annotation.point.y*bili);
        _lastBili = bili;
        
        
        
        if (self.isAnimation) {
            
            if (_canStartAnimation) {
                _canStartAnimation = NO;
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    sleep(0.1);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self startAnimationWith:_lastBili];
                        _canStartAnimation = YES;
                    });
                });
            }
        }
        else
        {
            
        }
        
    }
}





@end
